package net.maku.events.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import net.maku.events.vo.ActivityQuestionBankOperateVO;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.common.utils.Result;
import net.maku.events.service.ActivityQuestionBankService;
import net.maku.events.query.ActivityQuestionBankQuery;
import net.maku.events.vo.ActivityQuestionBankVO;
import net.maku.framework.operatelog.annotations.OperateLog;
import net.maku.framework.operatelog.enums.OperateTypeEnum;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * 题库题目
 *
 * @author 阿沐 babamu@126.com
 * @since 1.0.0 2025-03-22
 */
@RestController
@RequestMapping("events/activity_question_bank")
@Tag(name = "题库题目")
@AllArgsConstructor
public class ActivityQuestionBankController {
    private final ActivityQuestionBankService activityQuestionBankService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @PreAuthorize("hasAuthority('events:activity_question_bank:page')")
    public Result<PageResult<ActivityQuestionBankVO>> page(@ParameterObject @Valid ActivityQuestionBankQuery query) {
        PageResult<ActivityQuestionBankVO> page = activityQuestionBankService.page(query);

        return Result.ok(page);
    }

    @GetMapping("get/all/question")
    @Operation(summary = "分页")
    @PreAuthorize("hasAuthority('events:activity_question_bank:page')")
    public Result<List<ActivityQuestionBankVO>> page() {
        return Result.ok(activityQuestionBankService.getAllQuestion());
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('events:activity_question_bank:info')")
    public Result<ActivityQuestionBankOperateVO> get(@PathVariable("id") Long id) {
        return Result.ok(activityQuestionBankService.getInfoById(id));
    }

    @PostMapping
    @Operation(summary = "保存")
    @PreAuthorize("hasAuthority('events:activity_question_bank:save')")
    public Result<String> save(@RequestBody ActivityQuestionBankOperateVO vo) {
        activityQuestionBankService.save(vo);

        return Result.ok();
    }

    @PutMapping
    @Operation(summary = "修改")
    @PreAuthorize("hasAuthority('events:activity_question_bank:update')")
    public Result<String> update(@RequestBody @Valid ActivityQuestionBankOperateVO vo) {
        activityQuestionBankService.update(vo);
        return Result.ok();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @PreAuthorize("hasAuthority('events:activity_question_bank:delete')")
    public Result<String> delete(@RequestBody List<Long> idList) {
        activityQuestionBankService.deleteDataById(idList);

        return Result.ok();
    }

    @PostMapping("import")
    @Operation(summary = "导入题目")
    @OperateLog(type = OperateTypeEnum.IMPORT)
    @PreAuthorize("hasAuthority('events:activity_question_bank:import')")
    public Result<String> importExcel(@RequestParam("file") MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            return Result.error("请选择需要上传的文件");
        }
        activityQuestionBankService.importByExcel(file);

        return Result.ok();
    }

    @GetMapping("export/{type}")
    @Operation(summary = "导出题目")
    @OperateLog(type = OperateTypeEnum.EXPORT)
    @PreAuthorize("hasAuthority('events:activity_question_bank:export')")
    public void export(@PathVariable("type") Integer type) {
        activityQuestionBankService.export( type);
    }

}